package makeorder;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import datacontainers.Dish;
import datacontainers.Order;
import datacontainers.OrderItem;

public class OrderMaker {

    private Map<Dish, OrderItem> items = new HashMap<Dish, OrderItem>();

    private Double orderSumm = 0.0;

    public void inc(Dish dish) {
	OrderItem item = items.get(dish);
	if (item == null) {
	    items.put(dish, new OrderItem(dish, 0));
	    item = items.get(dish);
	}
	item.setCount(item.getCount() + 1);
	orderSumm += item.getDish().getCost();

    }

    public void del(Dish dish) {
	OrderItem item = items.get(dish);
	if (item != null) {
	    orderSumm -= dish.getCost() * item.getCount();
	    items.remove(dish);
	}
    }

    public Order getOrder() {
	Order order = new Order();
	Calendar calendar = GregorianCalendar.getInstance();
	calendar.setTime(new Date());
	calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
	calendar.set(Calendar.HOUR, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MILLISECOND, 0);
	order.setDate(calendar.getTime());

	order.setItems(new ArrayList<OrderItem>(items.values()));

	return order;
    }

    public Double getOrderSumm() {
	return orderSumm;
    }

}
